/*
 * Copyright 2002-2016 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.cert.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bris.niosocket.annotation.Interceptor;
import com.bris.niosocket.mapping.HandlerInterceptor;
import com.bris.niosocket.message.impl.RequestMessage;
import com.bris.niosocket.message.impl.ResponseMessage;

/**
 * TODO 描述该类型的作用</br>
 * 2016年2月24日</br>
 * @author xunaidong
 * @since 1.0.0
 */
@Interceptor(path="*")
public class ErrorHandlerInterceptor implements HandlerInterceptor {
	
	private final Logger log = LoggerFactory.getLogger(ErrorHandlerInterceptor.class);

	/* (non-Javadoc)
	 * @see org.springframework.core.Ordered#getOrder()
	 */
	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.HandlerInterceptor#preHandler(com.bris.niosocket.message.impl.RequestMessage, com.bris.niosocket.message.impl.ResponseMessage, java.lang.Object)
	 */
	@Override
	public void preHandler(RequestMessage request, ResponseMessage response,
			Object handler) throws Exception {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see com.bris.niosocket.mapping.HandlerInterceptor#aftertHandler(com.bris.niosocket.message.impl.RequestMessage, com.bris.niosocket.message.impl.ResponseMessage, java.lang.Object)
	 */
	@Override
	public void aftertHandler(RequestMessage request, ResponseMessage response,
			Object handler) throws Exception {
		log.debug("拦截器处理中!");
		if(!response.hasError()){
			response.pushUnique("ERRORCODE", "00000");
			response.pushUnique("ERRORMSG", "OK");
		}else{
			response.pushUnique("ERRORCODE", response.getErrorCode());
			response.pushUnique("ERRORMSG", response.getErrorMsg());
		}
	}

}
